package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.ReturnListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: ReturnListGoodsServiceImpl
 * Description:
 * Date: 2022/1/17 18:18
 *
 * @version:
 * @since JDK 1.8
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {



    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;



    @Autowired
    private SupplierDao supplierDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;


    @Override
    public void saveReturnListGoods(ReturnList returnList, String returnListGoodsStr, HttpSession session) {




        User user = (User)session.getAttribute("currentUser");

        returnList.setUserId(user.getUserId());

        returnListGoodsDao.saveReturnList(returnList);

        List<ReturnListGoods> returnListGoodsList = JSONObject.parseArray(returnListGoodsStr, ReturnListGoods.class);



        for (ReturnListGoods returnListGoods : returnListGoodsList) {
            returnListGoods.setReturnListId(returnList.getReturnListId());
            returnListGoodsDao.saveReturnListGoods(returnListGoods);
        }

    }


    @Override
    public List<ReturnList> getReturnListGoods(String returnNumber, Integer supplierId,Integer state, String sTime, String eTime) {
        List<ReturnList> returnListList = returnListGoodsDao.getReturnListGoods(returnNumber,supplierId,state,sTime,eTime);
        return returnListList;
    }

    @Override
    public List<ReturnListGoods> getGoodsList(Integer returnListId) {
        List<ReturnListGoods>returnListGoodsList = returnListGoodsDao.getGoodsList(returnListId);
        return returnListGoodsList;
    }

    @Override
    public void deleteReturnListGoods(Integer returnListId) {
        returnListGoodsDao.deleteReturnListGoods(returnListId);
        returnListGoodsDao.deleteReturnList(returnListId);
    }


    @Override
    public List<PurchaseListGoodsCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Integer> listGoodsIds = returnListGoodsDao.getGoodsList1(goodsTypeId,codeOrName);

        List<ReturnList> returnListList= returnListGoodsDao.getReturnListGoods(null, null,null,sTime,eTime);
        ArrayList<PurchaseListGoodsCount> purchaseListGoodsCountArrayList = new ArrayList<>();
        for (ReturnList returnList : returnListList) {
            String supplierName = supplierDao.selectById1(returnList.getSupplierId());
            List<ReturnListGoods> returnListGoodsList = returnListGoodsDao.getGoodsList(returnList.getReturnListId());
            for (ReturnListGoods returnListGoods : returnListGoodsList) {

                String goodsTypeName = goodsTypeDao.selectById(returnListGoods.getGoodsTypeId());

                PurchaseListGoodsCount purchaseListGoodsCount = new PurchaseListGoodsCount();

                purchaseListGoodsCount.setPurchaseListGoodsId(returnListGoods.getReturnListGoodsId());
                purchaseListGoodsCount.setNumber(returnList.getReturnNumber());
                purchaseListGoodsCount.setSupplierName(supplierName);
                purchaseListGoodsCount.setDate(returnList.getReturnDate());
                purchaseListGoodsCount.setCode(returnListGoods.getGoodsCode());
                purchaseListGoodsCount.setName(returnListGoods.getGoodsName());
                purchaseListGoodsCount.setModel(returnListGoods.getGoodsModel());
                purchaseListGoodsCount.setGoodsType(goodsTypeName);
                purchaseListGoodsCount.setUnit(returnListGoods.getGoodsUnit());
                purchaseListGoodsCount.setPrice(returnListGoods.getPrice());
                purchaseListGoodsCount.setNum(returnListGoods.getGoodsNum());
                purchaseListGoodsCount.setTotal(returnListGoods.getTotal());
                purchaseListGoodsCountArrayList.add(purchaseListGoodsCount);
            }
        }


        ArrayList<PurchaseListGoodsCount> purchaseListGoodsCountArrayList1 = new ArrayList<>();
        for (Integer listGoodsId : listGoodsIds) {
            for (PurchaseListGoodsCount purchaseListGoodsCount : purchaseListGoodsCountArrayList) {
                if(listGoodsId.equals(purchaseListGoodsCount.getPurchaseListGoodsId())){
                    purchaseListGoodsCountArrayList1.add(purchaseListGoodsCount);
                }
            }
        }
        return purchaseListGoodsCountArrayList1;
    }
}
